package com.app.http;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.app.model.bean.Bike;
import com.app.model.bean.Record;
import com.app.model.bean.TaskBean;
import com.app.model.bean.WalletDetail;
import com.app.model.response.GetWalletRes;
import com.app.model.response.InviteRes;
import com.app.model.response.LoginRes;
import com.app.model.response.ResObj;
import com.app.third.bugly.CrashReporterUtils;
import com.app.utils.FastJsonUtils;

import java.io.Serializable;
import java.util.HashMap;


/**
 * 数据解析类,负责根据不同请求解析相应数据
 */
public class HttpDataParser {
    private static final String LOG_TAG = HttpDataParser.class.getSimpleName();

    private static HashMap<String, Object[]> url_object_table = new HashMap<>();
    // 基本数据类型
    private static final int TYPE_OBJECT = 1;
    // 列表数据类型
    private static final int TYPE_LIST = 2;

    static {
        // 注册
        register(HttpDataReqUrl.URL_REGISTER, TYPE_OBJECT, LoginRes.class);
        // 登录
        register(HttpDataReqUrl.URL_LOGIN, TYPE_OBJECT, LoginRes.class);
        // 重置密码
        register(HttpDataReqUrl.URL_RESET_PWD, TYPE_OBJECT, LoginRes.class);
        /** 获取操作记录列表 */
        register(HttpDataReqUrl.URL_GET_RECORDS, TYPE_LIST, Record.class);
        /** 获取密码 */
        register(HttpDataReqUrl.URL_GET_PWD, TYPE_OBJECT, Bike.class);
        /** 获取钱包余额 */
        register(HttpDataReqUrl.URL_GET_WALLET, TYPE_OBJECT, GetWalletRes.class);
        /** 获取钱包明细 */
        register(HttpDataReqUrl.URL_GET_WALLET_DETAIL, TYPE_LIST, WalletDetail.class);
        /** 获取邀请码和邀请URL */
        register(HttpDataReqUrl.URL_GET_INVITE_CODE, TYPE_OBJECT, InviteRes.class);
        /** 获取任务 */
        register(HttpDataReqUrl.URL_GET_TASK, TYPE_OBJECT, TaskBean.class);
    }

    public static ResObj parse(String url, String data) {
        Log.e(LOG_TAG, "data: " + data);
        ResObj result = null;
        try {
            result = FastJsonUtils.json2Object(data, ResObj.class);
        } catch (Exception e) {
            Log.e(LOG_TAG, "数据解析异常: url = " + url + ",data = " + data);
            CrashReporterUtils.postCatchedException(new Throwable("数据解析异常: url = " + url + ",data = " + data));
        }
        if (null == result) {
            result = new ResObj();
        }
        return parseData(url, result);
    }

    private static void register(@NonNull String url, int type, @Nullable Class<? extends Serializable> cls) {
        if (TextUtils.isEmpty(url)) {
            throw new RuntimeException("url不能为空");
        }
        if (TYPE_OBJECT != type && TYPE_LIST != type) {
            throw new RuntimeException("type取值只能为: #TYPE_OBJECT 或 #TYPE_LIST");
        }
        Object[] value = new Object[]{type, cls};
        url_object_table.put(url, value);
    }

    // 数据解析优化实现类
    private static ResObj parseData(String url, ResObj result) {
        final Object[] values = url_object_table.get(url);
        if (null != values) {
            final Integer type = (Integer) values[0];
            Class<? extends Serializable> cls = (Class<? extends Serializable>) values[1];
            if (null == cls) cls = String.class;
            final Object data = result.getData();
            String str = null;
            if (null != data) {
                str = JSON.toJSONString(data);
            }
            if (!TextUtils.isEmpty(str)) {
                if (TYPE_OBJECT == type) {
                    result.setData(JSON.parseObject(str, cls));
                } else {
                    result.setData(JSON.parseArray(str, cls));
                }
            }
        }
        return result;
    }
}
